package com.projectName.www.dao;

import com.projectName.www.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ComplaintDaoImpl implements ComplaintDao{

    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;

    /**
     *
     * @param uid 用户id
     * @return list 查询结果
     */
    @Override
    public List<String> queryComplaintByUid(String uid) {
        List<String> remarkList=new ArrayList<>();
        try {

            connection = JDBCUtils.getConnection();//首先连接数据库

            String sql = "select * from complaint WHERE uid= ?";//写出sql语句

            preparedStatement.setString(1,uid);

            preparedStatement = connection.prepareStatement(sql);//执行sql语句

            resultSet = preparedStatement.executeQuery();//获得查询结果集



            while (resultSet.next()){
                String remark = resultSet.getString("complaints");
                remarkList.add(remark);
            }


        } catch (SQLException e) {
            throw new DaoException(e.getMessage(),e);
        } finally {
            JDBCUtils.close(resultSet,preparedStatement,connection);
        }
        return remarkList;
    }

    /**
     *
     * @param uid 用户id
     * @param cp 设置投书
     */
    @Override
    public void setComplaint(String uid, String cp) {


        try {
            connection = JDBCUtils.getConnection();//首先连接数据库


            String sql = "INSERT INTO complaint (uid,complaints) VALUES (?,?)";//写出sql语句

            preparedStatement=connection.prepareStatement(sql);//预编译sql

            preparedStatement.setString(1,uid);

            preparedStatement.setString(2,cp);

            preparedStatement.executeUpdate();


        } catch (SQLException e) {
            throw new DaoException(e.getMessage(),e);
        } finally {
            JDBCUtils.close(resultSet,preparedStatement,connection);
        }
    }
}
